#pragma once

#include <cstdio>
#include <cuda_runtime.h>

// CUDA alignment is set to 16 to support the largest load type that we have =>
// uint4 (4*4 bytes) => 128 bit
#define CUDA_ALIGNMENT 16

inline unsigned divUp(unsigned x, unsigned y) { return (x + y - 1) / y; }

#ifndef NDEBUG
#define CudaCheckError()
#else
#define CudaCheckError()                                                       \
  do {                                                                         \
    cudaError err_ = cudaGetLastError();                                       \
    if (err_ != cudaSuccess) {                                                 \
      printf("CudaCheckError() failed at: %s:%d\n", __FILE__, __LINE__);       \
      printf("code: %d ; description: %s\n", err_, cudaGetErrorString(err_));  \
      exit(1);                                                                 \
    }                                                                          \
                                                                               \
    err_ = cudaDeviceSynchronize();                                            \
    if (cudaSuccess != err_) {                                                 \
      printf("CudaCheckError() failed after sync at: %s:%d;\n", __FILE__,      \
             __LINE__);                                                        \
      printf("code: %d; description: %s\n", err_, cudaGetErrorString(err_));   \
      exit(1);                                                                 \
    }                                                                          \
  } while (0)
#endif

#define CudaCheck(CALL)                                                        \
  do {                                                                         \
    if (cudaSuccess != CALL) {                                                 \
      printf("Cuda error at  %s:%d with error %s(%i)\n", __FILE__, __LINE__,   \
             cudaGetErrorString(CALL), CALL);                                  \
      exit(CALL);                                                              \
    }                                                                          \
  } while (0);
